<?php
include('../fns_all.php');

function get_all_pics_ordered_by_date(){
	$cxn = db_connect();
	$query = " SELECT * FROM photos order by created_at desc ";
	$result = mysqli_query($cxn, $query) or die('Error, get_all_pics_ordered_by_date failed');
	return $result;
}



function delete_pic($id){
	$cxn = db_connect();
	
	$query = " SELECT tag_id FROM photos_taggings where photo_id='$id' ";
	$result = mysqli_query($cxn, $query) or die('Error, getting all tags for a photo failed');
	
	$myrow = mysqli_fetch_assoc ($result);
	do {
		
		$tag_id=$myrow['tag_id'];
		$query = " SELECT tag_id FROM photos_taggings where photo_id <> '$id' and tag_id = '$tag_id'";
		$result2 = mysqli_query($cxn, $query) or die('Error, getting all tags not for a photo failed');
		
		if(mysqli_num_rows($result2) == 0){
			mysqli_query ($cxn, "delete from photos_tags where id = '$tag_id'");	
		}
	}while ($myrow = mysqli_fetch_assoc ($result));
	
	mysqli_query ($cxn, "delete from photos_taggings where photo_id = '$id'");
	
	mysqli_query ($cxn, "delete from photos_comments where photo_id = '$id'");
	
	mysqli_query ($cxn, "delete from photos_votes where photo_id = '$id'");
	
	mysqli_query ($cxn, "delete from photos where id = '$id'");
	
	
	//print($_SERVER['DOCUMENT_ROOT']."/photos/photos/large/".$id.".jpg");
	//chdir($_SERVER['DOCUMENT_ROOT']."/photos/photos/large/");
	unlink($_SERVER['DOCUMENT_ROOT']."/photos/photos/original/".$id.".jpg");
	unlink($_SERVER['DOCUMENT_ROOT']."/photos/photos/small/".$id.".jpg");
	unlink($_SERVER['DOCUMENT_ROOT']."/photos/photos/medium/".$id.".jpg");
	unlink($_SERVER['DOCUMENT_ROOT']."/photos/photos/large/".$id.".jpg");
}
?>
